import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";

/**
 * 后处理组件
 */
export default class PostProcessLayerComponent {
    private static instance: PostProcessLayerComponent = new PostProcessLayerComponent();
    /**
     * 后处理组件
     */
    public static get Default(): PostProcessLayerComponent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);
    }

    public async AddVolumeAsync(isGlobal: boolean = true, blendDistance: number = 0, weight: number = 1, priority: number = 0): Promise<number> {
        const request = new ClientMessage.PostProcessVolumeAddRequest(isGlobal, blendDistance, weight, priority);
        const response: ClientMessage.PostProcessVolumeAddResponse = await WebSocketComponent.Default.PostMessageAsync(request);
        return response.Id;
    }

    public async RemoveVolumeAsync(id: number): Promise<boolean> {
        const request = new ClientMessage.PostProcessVolumeRemoveRequest(id);
        const response: ClientMessage.PostProcessVolumeRemoveResponse = await WebSocketComponent.Default.PostMessageAsync(request);
        return response.Error == 0;
    }

    public async RemoveEffectAsync(volume: number, effect: number): Promise<boolean> {
        const request = new ClientMessage.PostProcessEffectRemoveRequest(volume, effect);
        const response: ClientMessage.PostProcessEffectRemoveResponse = await WebSocketComponent.Default.PostMessageAsync(request);
        return response.Error == 0;
    }

    public async AddKawaseBlurAsync(volumn: number, setAllOverrides: boolean = true, blurRadius: number = 0.5, iteration: number = 6, rTDownScaling: number = 2): Promise<number> {
        const request = new ClientMessage.KawaseBlurAddRequest(volumn, setAllOverrides, blurRadius, iteration, rTDownScaling);
        const response: ClientMessage.KawaseBlurAddResponse = await WebSocketComponent.Default.PostMessageAsync(request);
        return response.Id;
    }
}